#include "RebootServer.h"
#include "Protocol/ProtocolApi.h"

using namespace Protocol;

RebootServer *RebootServer::s_RebootServer = nullptr;

using namespace Fdt;
extern "C" int SYS_SystemCall(const char *pCmd);


int DEV_ShutDown(void)
{
	return 0;
}
int DEV_Reboot(void)
{
	return 0;
}

void RebootServerInit()
{
	RebootServer::instance();
}

RebootServer::RebootServer(){
}


void RebootServer::RegistRPC(){
	RPCMethodMap method_map;
	method_map.emplace("Reboot", CRPCFuntion::CreateRpc([this](const Json::Value &request, const BufferMap::Ptr &inMap, Json::Value &response, BufferMap::Ptr &outMap){
		return Reboot(request, inMap, response, outMap);
	}));

	method_map.emplace("ShutDown", CRPCFuntion::CreateRpc([this](const Json::Value &request, const BufferMap::Ptr &inMap, Json::Value &response, BufferMap::Ptr &outMap){
		return ShutDown(request, inMap, response, outMap);
	}));
	
	IProtocolInterface::GetInstance()->RegistProtocol("DevicePwr", method_map);
}

bool RebootServer::Reboot(const Json::Value &request, const BufferMap::Ptr &inMap, Json::Value &response, BufferMap::Ptr &outMap){
	DEV_Reboot();
	response["data"]["Param"]["Result"] = true;
	return true;
}

bool RebootServer::ShutDown(const Json::Value &request, const BufferMap::Ptr &inMap, Json::Value &response, BufferMap::Ptr &outMap){
	sync();
	sleep(1);
	DEV_ShutDown();
	response["data"]["Param"]["Result"] = true;
	return true;
}

